<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
 <head>
  <title>课程修改</title>
  <t:base type="jquery,easyui,tools,DatePicker"></t:base>
 
 <script src="plug-in/jquery-plugs/form/jquery.form.js"></script>
  <script type="text/javascript" charset="utf-8" src="plug-in/ueditor/ueditor.config.js"></script>
   <script type="text/javascript" charset="utf-8" src="plug-in/ueditor/ueditor.all.min.js"> </script> 
   <script type="text/javascript" charset="utf-8" src="plug-in/ueditor/lang/zh-cn/zh-cn.js"></script>
   <script type="text/javascript" src="plug-in/Validform/js/Validform_v5.3.1_min_zh-cn.js"></script>
   <script type="text/javascript" src="plug-in/Validform/js/Validform_Datatype_zh-cn.js"></script>
   <script type="text/javascript" src="plug-in/Validform/js/datatype_zh-cn.js"></script>
   <link rel="stylesheet" href="plug-in/Validform/css/style.css" type="text/css">
   <link rel="stylesheet" href="plug-in/Validform/css/tablefrom.css" type="text/css">
 </head>
 
 <body>
		<input type="hidden" name="id" value="${obj.id }" />
		<table style="" cellpadding="0" cellspacing="1" class="formtable">
			<tr>
				<td align="right">
					<label class="Validform_label"> 项目编号:</label>
				</td>
				<td class="value">
					<label style="width: 150px">${obj.itemNo }</label>
				</td>
				<td align="right">
					<label class="Validform_label"> 项目名称:</label>
				</td>
				<td class="value">
					<label style="width: 150px">${obj.itemName }</label>
				</td>
				
				<td align="right">
					<label class="Validform_label"> 实验类型:</label>
				</td>
				<td class="value">
					<label style="width: 150px">${obj.testType.testTypeName }</label>
				</td>
			</tr>
			<tr>
				<td align="right">
					<label class="Validform_label"> 实验类别:	</label>
				</td>
				<td class="value">
					<label style="width: 150px">${obj.testCategory.categoryName }</label>
				</td>

				<td align="right">
					<label class="Validform_label"> 适用专业:</label>
				</td>
				<td class="value">
					<label style="width: 150px">${obj.applyMajor }</label>
				</td>
				
				<td align="right">
					<label class="Validform_label"> 实验要求:</label>
				</td>
				<td class="value">
					<label style="width: 150px">${obj.testRequire }</label>
				</td>
			</tr>
			<tr>
				<td align="right">
					<label class="Validform_label"> 总学时:</label>
				</td>
				<td class="value">
					<label style="width: 150px">${obj.planHour }</label>
				</td>
					
				<td align="right">
					<label class="Validform_label"> 计划组数:	</label>
				</td>
				<td class="value">
					<label style="width: 150px">${obj.groupCount }</label>
				</td>
				
				<td align="right">
					<label class="Validform_label">计划每组人数: </label>
				</td>
				<td class="value">
					<label style="width: 150px">${obj.averageCount }</label>
				</td>
			</tr>
		</table>
		
	<div style="padding-top:5px;height: 150px;">
		所需仪器设备
		<a class='myBtn roundBorder' onclick="addRow()">添加</a>
		<table class="commonTable" id='dataTable'>
			<thead>
				<tr>
					<th>名称</th>
					<th>型号</th>
					<th>规格</th>
					<th>单位</th>
					<th>数量</th>
					<th>操作</th>
				</tr>
			</thead> 
			<tbody>
	 			<c:forEach items="${rows}" var="row">
	 				<tr id="row_${row.id}" name="row">
		 				<td>${row.name }</td>
		 				<td>${row.model }</td>
		 				<td>${row.norm }</td>
		 				<td>${row.unit }</td>
		 				<td>${row.count }</td>
		 				<td>
							<a href='javascript:void(0)' onclick='deleteRow(this)'>删除</a>
						</td>
 					</tr>
	 			</c:forEach>
			</tbody>
		</table> 
	</div>	
	
	<div style="padding-top:5px;height: 150px;">
		所需低值易耗品
		<a class='myBtn roundBorder' onclick="addConsumeRow()">添加</a>
		<table class="commonTable" id=consume>
			<thead>
				<tr>
					<th>名称</th>
					<th>单位</th>
					<th>数量</th>
					<th>操作</th>
				</tr>
			</thead> 
			<tbody>
	 			<c:forEach items="${rows2}" var="row">
	 				<tr id="row_${row.id}" name="row">
		 				<td>${row.name }</td>
		 				<td>${row.unit }</td>
		 				<td>${row.count }</td>
		 				<td>
							<a href='javascript:void(0)' onclick='deleteRow(this)'>删除</a>
						</td>
 					</tr>
	 			</c:forEach>
			</tbody>
		</table> 
	</div>	
	<div style="text-align: center;">
		<button onclick="submit()">保存</button>
	</div>
 </body>  
 <script>
 	function addRow(){
 		$.dialog({
		  	id:'xxx',
			content: 'url:labProjectController.do?goDeviceAdd',
			lock : true,
			zIndex: getzIndex(), 
			parent:frameElement.api,
			width:480,
			height:350,
			title:'添加设备', 
			opacity : 0.3,
			cache:false,
		    ok: function(){
		    	iframe = this.iframe.contentWindow;
		    	var row = iframe.getData();
		    	
	    		var html = '';
	    		html += 
	    			'<tr>'+
	    				'<td name="name">'+ row.name +'</td>'+
	    				'<td name="model">'+ row.model +'</td>'+
	    				'<td name="norm">'+ row.norm +'</td>'+
	    				'<td name="unit">'+ row.unit +'</td>'+
	    				'<td name="count">'+ row.count +'</td>'+
	    				'<td>'+
	    					'<a href="javascript:void(0)" onclick="deleteRow(this)">删除</a>'+
	    				'</td>'+
	    			'</tr>';
	    		$('#dataTable tbody').append(html);

		    },
		    cancelVal: '关闭',
		    cancel: true /*为true等价于function(){}*/
	});
	}
 	
 	function addConsumeRow(){
 		$.dialog({
		  	id:'xxx',
			content: 'url:labProjectController.do?goConsumeAdd',
			lock : true,
			zIndex: getzIndex(), 
			parent:frameElement.api,
			width:480,
			height:350,
			title:'添加易耗品', 
			opacity : 0.3,
			cache:false,
		    ok: function(){
		    	iframe = this.iframe.contentWindow;
		    	var row = iframe.getData();
		    	
	    		var html = '';
	    		html += 
	    			'<tr>'+
	    				'<td name="name">'+ row.name +'</td>'+
	    				'<td name="unit">'+ row.unit +'</td>'+
	    				'<td name="count">'+ row.count +'</td>'+
	    				'<td>'+
	    					'<a href="javascript:void(0)" onclick="deleteRow(this)">删除</a>'+
	    				'</td>'+
	    			'</tr>';
	    		$('#consume tbody').append(html);

		    },
		    cancelVal: '关闭',
		    cancel: true /*为true等价于function(){}*/
	});
	}
 	
 	var delIds = [];
 	function deleteRow(obj){
		var tr = $($(obj).parent()).parent();
		var id = $(tr).attr('id') + '';
		if(id!=''&&id!=null&&id!='undefined'){
			delIds.push(id.split('_')[1]+'');
		}
		
		$(tr).remove();
	}
 	
 	var delConsumeIds = [];
 	function deleteConsumeRow(obj){
		var tr = $($(obj).parent()).parent();
		var id = $(tr).attr('id') + '';
		if(id!=''&&id!=null&&id!='undefined'){
			delConsumeIds.push(id.split('_')[1]+'');
		}
		
		$(tr).remove();
	}
 	
 	function getNewRows(){
 		var newRows = $("#dataTable tbody tr[name!='row']");
 		var rowArr = [];
 		for(var i=0;i<newRows.length;i++){
 			var name = $(newRows[i]).children('[name="name"]').text();
 			var model = $(newRows[i]).children('[name="model"]').text();
 			var norm = $(newRows[i]).children('[name="norm"]').text();
 			var unit = $(newRows[i]).children('[name="unit"]').text();
 			var count = $(newRows[i]).children('[name="count"]').text();
 			
 			var row = {};
 			row.name = name;
 			row.model = model;
 			row.norm = norm;
 			row.unit = unit;
 			row.count = count;
 			rowArr.push(row);
 		}
 		
 		return rowArr;
 	}
 	
 	function getConsumeNewRows(){
 		var newRows = $("#consume tbody tr[name!='row']");
 		var rowArr = [];
 		for(var i=0;i<newRows.length;i++){
 			var name = $(newRows[i]).children('[name="name"]').text();
 			var model = $(newRows[i]).children('[name="model"]').text();
 			var norm = $(newRows[i]).children('[name="norm"]').text();
 			var unit = $(newRows[i]).children('[name="unit"]').text();
 			var count = $(newRows[i]).children('[name="count"]').text();
 			
 			var row = {};
 			row.name = name;
 			row.model = model;
 			row.norm = norm;
 			row.unit = unit;
 			row.count = count;
 			rowArr.push(row);
 		}
 		
 		return rowArr;
 	}
	
	 function submit(){
		 var url = 'labProjectController.do?doProjectAdd';
		 $.ajax({
			 url: url,
			 type: 'post',
			 dataType: 'json',
			 data: {'newRows' : JSON.stringify(getNewRows()),
					'delIds':delIds.toString(),
					'id':$("[name='id']").val(),
					'newConsumeRows' : JSON.stringify(getConsumeNewRows()),
					'delConsumeIds':delConsumeIds.toString(),
				},
			 success: function(data){
				 if(data.success){
					 tip(data.msg);
					 location.reload();
				 }else{
					 tip(data.msg);
				 }
			 }
		 });
	 }
	 
 </script>
